package com.xyh.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyh.common.util.IPUtils;
import com.xyh.common.util.ResultCode;
import com.xyh.system.mapper.LoginLogMapper;
import com.xyh.system.model.LoginLog;
import com.xyh.system.service.ILoginLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 登录日志表 服务实现类
 * </p>
 *
 * @author kyle
 * @since 2021-01-05
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {

    @Override
    public Boolean saveLoginLog(String username, Integer status) {
        //登录日志
        LoginLog loginLog = new LoginLog();
        loginLog.setLoginStatus(ResultCode.SUCCESS.status().equals(status) ? "1" : "0");
        loginLog.setIp(IPUtils.getIpAddr());
        loginLog.setUsername(username);
        loginLog.setLoginTime(LocalDateTime.now());
        return baseMapper.insert(loginLog) > 0;
    }

    @Override
    public Page<LoginLog> getAllLoginLogByPage(Integer pageNumber, Integer pageSize, LoginLog loginLogQuery) {
        Page<LoginLog> page = new Page<>(pageNumber-1,pageSize);
        LambdaQueryWrapper<LoginLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like( loginLogQuery.getUsername() != null && !"".equals(loginLogQuery.getUsername()),LoginLog ::getUsername, loginLogQuery.getUsername());
        return baseMapper.selectPage(page, queryWrapper);
    }
}
